<ModalDialog
  {id}
  {label}
  {title}
  shrinkWidthToFit={true}
  background="var(--main-bg)"
>
  <GenericDialogList selectable={false} {items} on:click="onClick(event)"/>
</ModalDialog>
<script>
import ModalDialog from './ModalDialog.html'
import { store } from '../../../_store/store.js'
import GenericDialogList from './GenericDialogList.html'
import { show } from '../helpers/showDialog.js'
import { close } from '../helpers/closeDialog.js'
import { oncreate } from '../helpers/onCreateDialog.js'
import { setAccountBlocked } from '../../../_actions/block.js'
import { setAccountFollowed } from '../../../_actions/follow.js'
import { setShowReblogs } from '../../../_actions/setShowReblogs.js'
import { setDomainBlocked } from '../../../_actions/setDomainBlocked.js'
import { copyText } from '../../../_actions/copyText.js'
import { composeNewStatusMentioning } from '../../../_actions/mention.js'
import { toggleMute } from '../../../_actions/toggleMute.js'
import { reportStatusOrAccount } from '../../../_actions/report.js'
import { formatIntl } from '../../../_utils/formatIntl.js'
import { setAccountNotified } from '../../../_actions/setAccountNotified.js'

export default {
  oncreate,
  store: () => store,
  computed: {
    //
    // begin copypasta (StatusOptionsDialog.html / AccountProfileOptionsDialog.html)
    //
    verifyCredentialsId: ({ verifyCredentials }) => verifyCredentials.id,
    following: ({ relationship }) => relationship && relationship.following,
    followRequested: ({ relationship }) => relationship && relationship.requested,
    accountId: ({ account }) => account && account.id,
    acct: ({ account }) => account.acct,
    username: ({ account }) => account.username,
    muting: ({ relationship }) => relationship && relationship.muting,
    blocking: ({ relationship }) => relationship && relationship.blocking,
    notifying: ({ relationship }) => relationship && relationship.notifying,
    followLabel: ({ following, followRequested, account, username }) => {
      if (typeof following === 'undefined' || !account) {
        return ''
      }
      return (following || followRequested)
        ? formatIntl('intl.unfollowAccount', { account: `@${username}` })
        : formatIntl('intl.followAccount', { account: `@${username}` })
    },
    followIcon: ({ following, followRequested }) => (
      following ? '#fa-user-times' : followRequested ? '#fa-hourglass' : '#fa-user-plus'
    ),
    blockLabel: ({ blocking, username }) => (
      blocking
        ? formatIntl('intl.unblockAccount', { account: `@${username}` })
        : formatIntl('intl.blockAccount', { account: `@${username}` })
    ),
    blockIcon: ({ blocking }) => blocking ? '#fa-unlock' : '#fa-ban',
    muteLabel: ({ muting, username }) => (
      muting
        ? formatIntl('intl.unmuteAccount', { account: `@${username}` })
        : formatIntl('intl.muteAccount', { account: `@${username}` })
    ),
    muteIcon: ({ muting }) => muting ? '#fa-volume-up' : '#fa-volume-off',
    isUser: ({ accountId, verifyCredentialsId }) => accountId === verifyCredentialsId,
    //
    // end copypasta (StatusOptionsDialog.html / AccountProfileOptionsDialog.html)
    //
    showingReblogs: ({ relationship }) => relationship ? relationship.showing_reblogs : true,
    showReblogsLabel: ({ showingReblogs, username }) => (
      showingReblogs
        ? formatIntl('intl.hideReblogsFromAccount', { account: `@${username}` })
        : formatIntl('intl.showReblogsFromAccount', { account: `@${username}` })
    ),
    domain: ({ acct }) => acct.split('@')[1],
    blockingDomain: ({ relationship }) => relationship && relationship.domain_blocking,
    blockDomainLabel: ({ blockingDomain, domain }) => (
      blockingDomain
        ? formatIntl('intl.showDomain', { domain })
        : formatIntl('intl.hideDomain', { domain })
    ),
    reportLabel: ({ username }) => (
      formatIntl('intl.reportAccount', { account: `@${username}` })
    ),
    items: ({
      blockLabel, blocking, blockIcon, muteLabel, muteIcon,
      followLabel, followIcon, following, followRequested,
      accountId, verifyCredentialsId, username, isUser, showReblogsLabel,
      domain, blockDomainLabel, reportLabel, notifying
    }) => ([
      !isUser && {
        key: 'mention',
        label: formatIntl('intl.mentionAccount', { account: `@${username}` }),
        icon: '#fa-comments'
      },
      !isUser && !blocking && {
        key: 'follow',
        label: followLabel,
        icon: followIcon
      },
      !isUser && following && notifying === false && { // notifying could be undefined for old servers
        key: 'notify',
        label: formatIntl('intl.notify', { account: `@${username}` }),
        icon: '#fa-bell'
      },
      !isUser && following && notifying === true && { // notifying could be undefined for old servers
        key: 'denotify',
        label: formatIntl('intl.denotify', { account: `@${username}` }),
        icon: '#fa-bell-o'
      },
      !isUser && {
        key: 'block',
        label: blockLabel,
        icon: blockIcon
      },
      !isUser && !blocking && {
        key: 'mute',
        label: muteLabel,
        icon: muteIcon
      },
      !isUser && following && {
        key: 'showReblogs',
        label: showReblogsLabel,
        icon: '#fa-retweet'
      },
      !isUser && domain && {
        key: 'blockDomain',
        label: blockDomainLabel,
        icon: '#fa-ban'
      },
      !isUser && {
        key: 'report',
        label: reportLabel,
        icon: '#fa-flag'
      },
      {
        key: 'copy',
        label: 'intl.copyLinkToAccount',
        icon: '#fa-link'
      }
    ].filter(Boolean))
  },
  methods: {
    show,
    close,
    onClick (item) {
      switch (item.key) {
        case 'mention':
          return this.onMentionClicked()
        case 'follow':
          return this.onFollowClicked()
        case 'block':
          return this.onBlockClicked()
        case 'mute':
          return this.onMuteClicked()
        case 'showReblogs':
          return this.onShowReblogsClicked()
        case 'blockDomain':
          return this.onBlockDomainClicked()
        case 'copy':
          return this.onCopyClicked()
        case 'report':
          return this.onReport()
        case 'notify':
          return this.onNotifyClicked()
        case 'denotify':
          return this.onDenotifyClicked()
      }
    },
    async onMentionClicked () {
      const { account } = this.get()
      this.close()
      await composeNewStatusMentioning(account)
    },
    async onFollowClicked () {
      const { accountId, following } = this.get()
      this.close()
      await setAccountFollowed(accountId, !following, true)
    },
    async onBlockClicked () {
      const { accountId, blocking } = this.get()
      this.close()
      await setAccountBlocked(accountId, !blocking, true)
    },
    async onMuteClicked () {
      const { account, muting } = this.get()
      this.close()
      await toggleMute(account, !muting)
    },
    async onShowReblogsClicked () {
      const { accountId, showingReblogs } = this.get()
      this.close()
      await setShowReblogs(accountId, !showingReblogs, true)
    },
    async onBlockDomainClicked () {
      const { accountId, domain, blockingDomain } = this.get()
      this.close()
      await setDomainBlocked(accountId, domain, !blockingDomain, true)
    },
    async onCopyClicked () {
      const { account } = this.get()
      const { url } = account
      this.close()
      await copyText(url)
    },
    async onReport () {
      const { account } = this.get()
      this.close()
      await reportStatusOrAccount({ account })
    },
    async onNotifyClicked () {
      const { accountId } = this.get()
      this.close()
      await setAccountNotified(accountId, /* notify */ true, /* toastOnSuccess */ true)
    },
    async onDenotifyClicked () {
      const { accountId } = this.get()
      this.close()
      await setAccountNotified(accountId, /* notify */ false, /* toastOnSuccess */ true)
    }
  },
  components: {
    ModalDialog,
    GenericDialogList
  }
}
</script>
